﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.Core.Web.Controls
{
    [DefaultProperty("Title")]
    [ToolboxData("<{0}:ToolStripIcon runat=server></{0}:ToolStripIcon>")]
    public class ToolStripIcon : ImageButton
    {
        [Category("Appearance")]
        public string Title
        {
            get
            {
                object obj = ViewState["Title"];
                if (obj == null) { return string.Empty; }
                return (string)obj;
            }
            set { ViewState["Title"] = value; }
        }

        [Category("Behaviour")]
        public bool ShowTitle
        {
            get
            {
                object obj = ViewState["ShowText"];
                if (obj == null) { return false; }
                return (bool)obj;
            }
            set { ViewState["ShowText"] = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            this.CommandName = this.ID;
            this.Text = "";
            base.OnLoad(e);
            this.CommandName = this.ID;
            this.Width = new Unit(16, UnitType.Pixel);
            this.ImageAlign = ImageAlign.AbsMiddle;
            this.Height = new Unit(16, UnitType.Pixel);
            this.Style.Add("padding", "3px");
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute("class", this.CssClass);
            writer.AddAttribute("onmouseover", "this.className='" + this.CssClass + "Hover';");
            writer.AddAttribute("onmouseout", "this.className='" + this.CssClass + "';");

            if (!this.ShowTitle)
            {
                writer.AddStyleAttribute("width", "23px");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.RenderBeginTag(HtmlTextWriterTag.Center);
                base.Render(writer);
                writer.RenderEndTag();
                writer.RenderEndTag();
            }
            else
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                base.Render(writer);
                writer.AddAttribute("onclick", string.Format("document.getElementById('{0}').click();", this.ClientID));
                writer.AddAttribute("title", this.ToolTip);
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(this.Title);
                writer.RenderEndTag();
                writer.RenderEndTag();
            }

        }

    }
}
